﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccessLayer
{
    public class StationeryRepository
    {
        StationeryInventoryEntities1 ent;

        public StationeryRepository()
        {
            ent = new StationeryInventoryEntities1();
        }

        #region Select Method

        public List<Stationery> GetAllStationery()
        {
            List<Stationery> list = new List<Stationery>();

            var stat = ent.Stationeries;

            if (stat != null)
            {
                list = stat.ToList();
            }

            return list;
        }

        public List<Stationery> GetStationeryByCategoryId(int categoryId)
        {
            List<Stationery> list = new List<Stationery>();

            var stat = ent.Stationeries.Where(x => x.Category.CategoryID == categoryId);

            if (stat != null)
            {
                list = stat.ToList();
            }

            return list;
        }

        public List<Stationery> GetAllLowStockStationery()
        {
            List<Stationery> list = new List<Stationery>();

            var stat = ent.Stationeries.Where(x => x.AvailableQty <= x.ReorderLevel);

            if (stat != null)
            {
                list = stat.ToList();
            }

            return list;
        }

        public List<Stationery> GetLowStockStationeryByCategoryId(int categoryId)
        {
            List<Stationery> list = new List<Stationery>();

            var stat = ent.Stationeries.Where(x => x.Category.CategoryID == categoryId && x.AvailableQty <= x.ReorderLevel);

            if (stat != null)
            {
                list = stat.ToList();
            }

            return list;
        }

        public Stationery GetStationeryByItemCode(string itemCode)
        {
            Stationery stationery = new Stationery();

            var stat = ent.Stationeries.FirstOrDefault(x => x.ItemCode == itemCode);

            if (stat != null)
            {
                stationery = stat;
            }

            return stationery;
        }

        #endregion

        #region Update Method

        public void UpdateStationery(Stationery stationery)
        {
            var stat = GetStationeryByItemCode(stationery.ItemCode);

            stat.CategoryID = stationery.CategoryID;
            stat.ItemName = stationery.ItemName;
            stat.UOM = stationery.UOM;
            stat.ReorderLevel = stationery.ReorderLevel;
            stat.ReorderQty = stationery.ReorderQty;
            stat.BinNo = stationery.BinNo;

            ent.SaveChanges();
        }

        #endregion
    }
}
